﻿using System;
using fastCSharp.code.cSharp;
using fastCSharp;
using fastCSharp.net;

namespace diantou.web.ajax
{
    /// <summary>
    /// 公共Ajax调用
    /// </summary>
    [fastCSharp.code.cSharp.ajax(IsPool = true)]
    class pub : fastCSharp.code.cSharp.ajax.call<pub>, webView.IWebView
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="verify">客户端验证</param>
        /// <param name="isRemember">是否记住密码</param>
        /// <param name="error">错误提示</param>
        /// <returns></returns>
        public byte Login(string email, string password, string verify, bool isEnterprise, bool isRemember, out string error)
        {
            diantou.dataAccess.user.loginStatusType type;
            diantou.dataAccess.user user = login(email, password, verify, isEnterprise, isRemember, out type);
            error = fastCSharp.Enum<diantou.dataAccess.user.loginStatusType, diantou.dataAccess.user.loginStatus>.Array(type).Text;
            return user != null ? (byte)user.Type : (byte)0;
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="verify">客户端验证</param>
        /// <param name="isRemember">是否记住密码</param>
        /// <param name="type">用户登陆状态</param>
        /// <returns></returns>
        private diantou.dataAccess.user login(string email, string password, string verify, bool isEnterprise, bool isRemember, out diantou.dataAccess.user.loginStatusType type)
        {
            type = diantou.dataAccess.user.loginStatusType.UserPasswordError;
            if (verify != null && verify.Length != 0 && email != null)
            {
                diantou.dataAccess.user value = diantou.dataAccess.clientCache.User[diantou.dataAccess.tcpCall.user.getId(email)];
                if (value != null && (!isEnterprise || (byte)value.Type >= (byte)dataModel.user.type.未审核企业))
                {
                    type = dataAccess.user.loginStatusType.EmailVerify;
                    if (value.RegisterVerify == 0)
                    {
                        type = diantou.dataAccess.user.loginStatusType.UserPasswordError;
                        if (value.Password.Length != 0 && diantou.dataAccess.user.Md5Password(GetPassword(password, verify)) == value.Password)
                        {
                            if (value.IsLock) type = diantou.dataAccess.user.loginStatusType.Lock;
                            else
                            {
                                web.pub.Login(this, value, isEnterprise, isRemember, isRemember ? requestHeader.Host.simpleEqual(ref index.ExamDomain) : false);
                                type = diantou.dataAccess.user.loginStatusType.Login;
                                return value;
                            }
                        }
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// 验证密码还原
        /// </summary>
        /// <param name="password"></param>
        /// <param name="verify">客户端验证</param>
        /// <returns></returns>
        public unsafe static string GetPassword(string password, string verify)
        {
            if (password != null && password.Length != 0 && verify != null && verify.Length != 0)
            {
                int length = password.Length / 3;
                if (length + (length << 1) == password.Length)
                {
                    char* value = stackalloc char[length];
                    fixed (char* passwordFixed = password, verifyFixed = verify)
                    {
                        int index = 0, verifyIndex = 0, code;
                        for (char* start = passwordFixed, end = passwordFixed + password.Length, verifyRead = verifyFixed, write = value; start != end; *write++ = (char)code)
                        {
                            code = (*start++ - 48) << 10;
                            code |= (*start++ - 48) << 5;
                            code |= *start++ - 48;
                            code ^= *verifyRead++;
                            code ^= index;
                            if (++verifyIndex == verify.Length)
                            {
                                verifyIndex = 0;
                                verifyRead = verifyFixed;
                                ++index;
                            }
                        }
                    }
                    return new string(value, 0, length);
                }
            }
            return null;
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        public void Logout()
        {
            diantou.web.pub.Logout(this);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="email"></param>
        /// <param name="onReturn"></param>
        public void GetPassword(string email, Action<returnValue<bool>> onReturn)
        {
            diantou.dataAccess.tcpCall.user.getPassword(email, onReturn);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="onReturn"></param>
        /// <returns></returns>
        public void Register(diantou.dataAccess.user value, string verify, Action<returnValue<bool>> onReturn)
        {
            value.Password = GetPassword(value.Password, verify);
            diantou.dataAccess.tcpCall.user.register(value, onReturn);
        }
        /// <summary>
        /// 获取状态验证字符串
        /// </summary>
        /// <returns></returns>
        public string GetOpenApiState()
        {
            return fastCSharp.openApi.state.GetString();
        }
        /// <summary>
        /// 获取访问令牌
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="randomPrefix"></param>
        /// <param name="timeoutSeconds"></param>
        /// <param name="onReturn"></param>
        [fastCSharp.code.cSharp.ajax(IsReferer = false)]
        public void GetToken(string email, string password, string randomPrefix, int timeoutSeconds, Action<fastCSharp.net.returnValue<fastCSharp.openApi._51nod.returnValue<string>.value>> onReturn)
        {
            diantou.dataAccess.tcpCall.user.getToken(email, password, randomPrefix, timeoutSeconds, onReturn);
        }

        /// <summary>
        /// 获取手机验证码
        /// </summary>
        /// <param name="examId">企业考试标识</param>
        /// <param name="mobile">手机号</param>
        /// <param name="onReturn"></param>
        public void GetExamMobileCode(int examId, long mobile, Action<fastCSharp.net.returnValue<int>> onReturn)
        {
            diantou.examDataAccess.tcpCall.examUser.getMobileCode(examId, mobile, onReturn);
        }
        /// <summary>
        /// 企业考试登录
        /// </summary>
        /// <param name="examId">企业考试标识</param>
        /// <param name="mobile">手机号</param>
        /// <param name="mobileCode">手机验证码</param>
        /// <param name="email">邮箱地址</param>
        /// <returns></returns>
        public int LoginExam(int examId, long mobile, string mobileCode, string email, Action<fastCSharp.net.returnValue<int>> onReturn)
        {
            int userId;
            int value = diantou.examDataAccess.tcpCall.examUser.login(examId, mobile, mobileCode, email, out userId);
            if (userId > 0) web.pub.ExamLogin(this, userId, mobile);
            return value;
        }
    }
}
